﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VaccineEbook.Models;
using VaccineEbook.Models.DBEntity;

namespace VaccineEbook.Controllers
{
    public class VaccineController : Controller
    {
        string _messageError;
        //
        // GET: /Vaccine/

        public ActionResult Index(int? page)
        {
            string searchString = Request.QueryString[Constant.QUICK_SEARCH_STRING];
            ViewBag.MessageSuccess = TempData["MessageSuccess"];
            _messageError = (String)TempData["MessageError"] ?? "";

            IEnumerable<Vaccine> listVaccine = Vaccine.QuickSearch(searchString, ref _messageError);
            if (listVaccine != null)
            {
                int intPage = page ?? 1;
                CustomPageList<Vaccine> pagelist = new CustomPageList<Vaccine>(listVaccine, intPage, Models.Constant.DEFAUT_PAGESIZE);
                return View(pagelist);
            }

            ViewBag.MessageError = _messageError;
            return View();
        }

        [HttpGet]
        public ActionResult EditVaccine(string VaccineId)
        {
            Vaccine vaccine = new Vaccine();
            if (!string.IsNullOrEmpty(VaccineId))
            {
                // edit vaccine
                int intVaccineId = Common.ConvertUtils.ToInt32(VaccineId);
                vaccine = Vaccine.GetBy_VaccineID(intVaccineId, ref _messageError);

                ModelState.AddModelError("", _messageError);
            }
            return PartialView("_EditVaccinePartial", vaccine);
        }

        [HttpPost]
        public ActionResult EditVaccine(Vaccine vaccine)
        {
            if (vaccine.VaccineID == 0)
            {
                // add Vaccine
                string username = System.Web.HttpContext.Current.User.Identity.Name;
                Models.DBEntity.User user = Models.DBEntity.User.GetBy_Username(username, ref _messageError);

                vaccine = Vaccine.AddNew(vaccine, user.UserID, ref _messageError);
            }
            else
            {
                // update Vaccine
                vaccine = Vaccine.Update(vaccine.VaccineID, vaccine.VaccineName, ref _messageError);
            }

            if (vaccine != null)
            {
                // thành công
                //TempData["MessageSuccess"] = "Sucessfully";
                return Json(new { success = true }); 
            }
            else
            {
                // thất bại
                ModelState.AddModelError("", _messageError);
                return PartialView("_EditVaccinePartial", vaccine);
            }
        }

        [HttpPost]
        public ActionResult DeleteVaccine(string id)
        {
            Vaccine vaccine = new Vaccine();
            if (!string.IsNullOrEmpty(id))
            {
                // edit vaccine
                int vaccineId = Common.ConvertUtils.ToInt32(id);
                vaccine = Vaccine.Delete(vaccineId, ref _messageError);

                TempData["MessageError"] = _messageError;
            }
            return RedirectToAction("Index");
        }
    }
}
